<template>
  <div class="dialog">
    <el-dialog :title="'环境数据录入'" :visible.sync="showDialog" width="45%"  :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="rgiht" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="录入类型" prop="dataItemId">
          <el-select class="w100p" filterable clearable v-model="typeFlag" placeholder="请选择" @change="changeDataItem">
            <el-option label="手动数据项类型" :value="true">
            </el-option>
            <el-option label="系统数据项类型" :value="false">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="!typeFlag" label="选择鱼塘">
          <el-select class="w100p" filterable clearable v-model="fishPondId" @change="getTableDataList">
            <el-option v-for="(item, index) in fishPondList" :key="index" :label="item.pondName"
              :value="item.fishPondId" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="!typeFlag" label="数据项选择" prop="dataItemId">
          <el-select class="w100p" filterable clearable v-model="formData.dataItemId" placeholder="请选择">
            <el-option v-for="(item, index) in dataItemList_xt" :label="item.dataItemName" :value="item.dataItemId"
              :key="index + 'dataItem'" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="typeFlag" label="手动数据项" prop="dataItemId">
          <el-select class="w100p" filterable clearable v-model="formData.dataItemId" placeholder="请选择">
            <el-option v-for="(item, index) in dataItemList" :label="item.dataItemName" :value="item.dataItemId"
              :key="index + 'dataItem'" />
          </el-select>
        </el-form-item>
        <el-form-item label="采集值" prop="value">
          <el-input class="w100p" v-model="formData.value" placeholder="请输入采集数据" clearable />
        </el-form-item>
        <el-form-item label="采集时间" prop="collectDatetime">
          <el-date-picker v-model="formData.collectDatetime" type="datetime" placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DataItemManageApi from "@/api/basic-info-manage/data-item-manage";
import EnvironmentalDataEntryApi from "@/api/iot-monitoring/environmental-data-entry";
import EnvironmentalDataMonitoringApi from "@/api/iot-monitoring/environmental-data-monitoring";     // 折线图获取
import FishPondApi from "@/api/basic-info-manage/fishPond";
import { dialogMixin } from "@/utils/dialogMixin";

export default {
  name: '',
  mixins: [dialogMixin],
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
  },
  data() {
    return {
      typeFlag: true,
      formData: {
        xaxis: 50,
        yaxis: 50
      },
      fishPondId: '',
      fishPondList: [],
      rules: {
        dataItemId: [{ required: true, message: "请选择数据项", trigger: "blur" }],
        value: [{ required: true, message: "请输入采集数据", trigger: "blur" }],
        collectDatetime: [{ required: true, message: "请选择采集时间", trigger: "blur" }],
      },
      // 采控标识
      dataItemList: [],
      dataItemList_xt: [],
      // 值类型
      valueType: [],
      showDialog: this.show,
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          collectDatetime: this.getNowTime()
        }
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    },
  },
  created() {
  },
  mounted() {
    this.queryDataItem();
    this.queryFishPond();
  },
  methods: {
    changeDataItem() {
      this.$set(this.formData, 'dataItemId', '')
    },
    queryDataItem() {
      DataItemManageApi.queryByCondition({ whetherAutomatic: false }).then(res => {
        if (res.success) this.dataItemList = res.data;
      })
    },
    // 获取数据类型
    getTableDataList(e) {
      this.dataItemList_xt = []
      if (e) {
        EnvironmentalDataMonitoringApi.queryNewByFishPondId({ fishPondId: e }).then(res => {
          let dataList = res.data || [];
          let dataItemList = dataList.filter(item => {
            return item.dataType && item.dataType.mainingControlType == "COLLECTION"
          })
          if (res.success && dataItemList.length > 0) {
            this.$set(this.formData, 'dataItemId', dataItemList[0].dataItemId)
            this.dataItemList_xt = dataItemList;
          } else {
            this.dataItemList_xt = []
            this.$set(this.formData, 'dataItemId', '')
          }
        });
      } else {
        this.$set(this.formData, 'dataItemId', '')
      }
    },
    //查询可选鱼塘
    queryFishPond() {
      FishPondApi.queryByCondition().then(res => {
        this.fishPondList = res.data;
        if (this.fishPondList.length > 0) {
          this.fishPondId = this.fishPondList[0].fishPondId;
          this.getTableDataList(this.fishPondId)
        }
      });
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            EnvironmentalDataEntryApi.edit(this.formData).then(res => {
              if (res.success) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error("系统错误,修改失败");
              }
            });
          } else if (this.nowChanceStau === "add") {
            EnvironmentalDataEntryApi.add(this.formData).then(res => {
              if (res.success) {
                this.$message.success("添加成功");
                this.close()
              } else {
                this.$message.error("系统错误,添加失败");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
      this.address = []
    },
    cancel() {
      this.$emit("cancelDialog");
      this.address = []
      this.$refs.ruleForm.resetFields()
    }
  }
};
</script>

<style lang="scss" scoped>
.line-click {
  width: 25px;
  height: 25px;
  // background-color: aqua;
  display: flex;
  justify-content: center;
  align-items: center;
  border: snow 1px solid;
  border-radius: 50%;
  margin-bottom: 10px;
  margin-top: 5px;
  margin-left: 5px;
  color: #fff;
  background-color: #003947;
  cursor: pointer;
}

.hang-content {
  width: 100%;

  .hang-content-one {
    width: 100%;
    display: flex;
  }
}

.pot-xy {
  width: 1300px;
  max-width: 1300px;
  height: 600px;
  min-height: 600px;
  background-image: url("~@/assets/oth-img/fish-pond-background.png");
  background-repeat: no-repeat;
  background-position: top;
  background-size: 1300px 100%;
  position: relative;

  .mczilong {
    height: 80px;
    line-height: 80px;
    background-color: #1fce4b;
    color: #fff;
    white-space: nowrap;
    text-align: center;
    cursor: pointer;
    font-weight: 600;
    font-size: 20px;
  }
}
</style>
